package org.example.gametalk.ServiceImpl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.example.gametalk.Entity.User;
import org.example.gametalk.Mapper.UserMapper;
import org.example.gametalk.Service.LoginService;
import org.example.gametalk.gameTalkUtils.JwtUtils;
import org.example.gametalk.gameTalkUtils.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class LoginServiceImpl implements LoginService {
    @Autowired
    UserMapper userMapper;
    @Override
    public Response<Object> login(User user) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getAccount, user.getAccount());
        User user1 = userMapper.selectOne(wrapper);

        if(user1 == null) {
            return Response.fail("该账号未注册，请注册");
        }

        if(!user.getPassword().equals(user1.getPassword())) {
            return Response.fail("密码错误");
        }

        String token = JwtUtils.getToken(user.getId());
        /*TODO 存储token
        可选方式:
        springboot cache存储
        redis 存储
         */
        return Response.success("成功登录").setData(token);
    }

    private void cacheToken8Local(String token) {

    }

    private void cacheToken8Redis(String token) {}
}
